﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Manipulation;
using ITAP.ShprossesBuilder.Modelling;

namespace ITAP.ShprossesBuilder.Editor
{
    public partial class CommandStackForm : Form, IModelVisitor
    {
        private bool disableSelectedIndexChangedEvent = false;
        private ModelManipulator manipulator;

        public ModelManipulator Manipulator
        {
            get { return manipulator; }
            set { manipulator = value; }
        }

        public CommandStackForm(ModelManipulator manipulator, Form owner)
        {
            InitializeComponent();
            this.Manipulator = manipulator;
            this.Owner = owner;

        }

        public void Show()
        {            
            this.RebuildListView();
            base.Show();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void listBoxCommands_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(!disableSelectedIndexChangedEvent)
                this.manipulator.SetCurrentHistoryIndex(listBoxCommands.SelectedIndex);
        }

        private void listBoxCommands_SelectedValueChanged(object sender, EventArgs e)
        {

        }


        #region IModelVisitor Members

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Model model)
        {
            throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Connector connector)
        {
            throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Shpross shpross)
        {
            throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.Spacer spacer)
        {
            throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnectorStub stub)
        {
            throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossConnector shprossConnector)
        {
            throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerElement spacerElement)
        {
            throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerConnector spacerConnector)
        {
            throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelCell modelCell)
        {
            throw new NotImplementedException();
        }

        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ModelContainer modelContainer)
        {
            this.RebuildListView();
      //      throw new NotImplementedException();
        }

        #endregion

        private void RebuildListView()
        {
            disableSelectedIndexChangedEvent = true;
            if (manipulator == null)
                return;
            listBoxCommands.BeginUpdate();
            int selectedIndex = listBoxCommands.SelectedIndex;
            listBoxCommands.Items.Clear();
            foreach (ICommand command in manipulator.History)
                listBoxCommands.Items.Add(command);
            listBoxCommands.SelectedIndex = manipulator.CurrentHistoryElementIndex;
            listBoxCommands.EndUpdate();
            disableSelectedIndexChangedEvent = false;
        }

        private void CommandStackForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Visible = false;
        }

        #region IModelVisitor Members


        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.StickConnector stickConnector)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SnapOnCap snapOnCap)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossEndToEndConnector endToEndConnector)
        {
          //  throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.ShprossChain shprossChain)
        {
     //       throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.SpacerContainer spacerContainer)
        {
          //  throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ITAP.ShprossesBuilder.Modelling.Components.CrossConnector crossConnector)
        {
      //      throw new NotImplementedException();
        }

        #endregion
    }
}
